package com.example.gslih.componentproject.main.slideconflict;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.gslih.componentproject.R;

import java.util.ArrayList;

import jp.wasabeef.recyclerview.adapters.AlphaInAnimationAdapter;
import jp.wasabeef.recyclerview.adapters.SlideInBottomAnimationAdapter;
import jp.wasabeef.recyclerview.animators.FlipInBottomXAnimator;
import jp.wasabeef.recyclerview.animators.LandingAnimator;
import jp.wasabeef.recyclerview.animators.OvershootInLeftAnimator;
import jp.wasabeef.recyclerview.animators.ScaleInLeftAnimator;
import jp.wasabeef.recyclerview.animators.SlideInLeftAnimator;

public class SlidingConflictActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sliding);

        ArrayList<String> data = new ArrayList<>();
        for (int i=0;i<40;i++){
            data.add(i+"");
        }

        final NewScrollerView scrollView = findViewById(R.id.slice_scroll);
        //设置空界面
        final EmptyRecyclerView recyclerView = findViewById(R.id.rv_slide_conflict);

        final RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this,2);
        recyclerView.setLayoutManager(layoutManager);
        //数据为空时需要显示的界面
        View emptyView = LayoutInflater.from(SlidingConflictActivity.this).inflate(R.layout.empty_rv,recyclerView,false);
        recyclerView.setEmptyView(emptyView);
        //装饰器模式的adapter
        final RvAdapter rvAdapter = new RvAdapter(data);
        final RvAdapterWrapper rvAdapterWrapper = new RvAdapterWrapper(rvAdapter);
        View header = LayoutInflater.from(SlidingConflictActivity.this).inflate(R.layout.rv_header,recyclerView,false);
        View footer = LayoutInflater.from(SlidingConflictActivity.this).inflate(R.layout.rv_footer,recyclerView,false);
        rvAdapterWrapper.addHeaderView(header);
        rvAdapterWrapper.addFooterView(footer);
        recyclerView.setAdapter(rvAdapter);
        //设置recyclerview长按可以移动位置
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new SimpleItemTouchCallback(rvAdapterWrapper));
        itemTouchHelper.attachToRecyclerView(recyclerView);

        Button button = findViewById(R.id.slide_title);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                layoutManager.scrollToPosition(rvAdapterWrapper.getItemCount()-1);
//                layoutManager.scrollToPosition(rvAdapter.getItemCount());
                scrollView.fullScroll(ScrollView.FOCUS_DOWN);
            }
        });

        Button button1 = findViewById(R.id.slide_end);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scrollView.fullScroll(ScrollView.FOCUS_UP);
                layoutManager.scrollToPosition(0);
            }
        });

        Button add = findViewById(R.id.slide_add);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rvAdapterWrapper.addNum();
//                rvAdapter.addItemView();
                layoutManager.scrollToPosition(0);
            }
        });

        Button delete = findViewById(R.id.slide_delete);
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rvAdapterWrapper.delete();
//                rvAdapter.deleteItem();
                layoutManager.scrollToPosition(0);
            }
        });


    }

    //第一处理数据的adapter
    class RvAdapter extends RecyclerView.Adapter{

        public ArrayList<String> datas;

        public RvAdapter(ArrayList<String> datas) {
            this.datas = datas;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_slide,parent,false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.textView.setText(datas.get(position));
        }

        @Override
        public int getItemCount() {
            return datas.size();
        }

        public void addItemView(){
            datas.add(0,"new item"+datas.size());
            Log.d("=========", "addItemView: "+datas.size());
            notifyItemInserted(0);
//            notifyDataSetChanged();
        }

        public void deleteItem(){
            datas.remove(0);
            Log.d("=========", "deleteItem: "+datas.size());
            notifyItemRemoved(0);
//            notifyDataSetChanged();

        }


        class ViewHolder extends RecyclerView.ViewHolder{

            TextView textView;
            public ViewHolder(View itemView) {
                super(itemView);
                textView = itemView.findViewById(R.id.tv_item_slide);
            }
        }
    }

    //第一adapter的装饰器
    static class RvAdapterWrapper extends RecyclerView.Adapter{

        enum ITEM_TYPE{
            HEADER,
            FOOTER,
            NORMAL
        }

        private RvAdapter rvAdapter;
        private View mHeaderView;
        private View mFooterView;

        public RvAdapterWrapper(RvAdapter rvAdapter) {
            this.rvAdapter = rvAdapter;
        }

        @Override
        public int getItemViewType(int position) {
            if(position == 0){
                return ITEM_TYPE.HEADER.ordinal();
            } else if(position == rvAdapter.getItemCount() + 1){
                return ITEM_TYPE.FOOTER.ordinal();
            } else{
                return ITEM_TYPE.NORMAL.ordinal();
            }
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            if(viewType == ITEM_TYPE.HEADER.ordinal()){
                return new RecyclerView.ViewHolder(mHeaderView) {};
            } else if(viewType == ITEM_TYPE.FOOTER.ordinal()){
                return new RecyclerView.ViewHolder(mFooterView) {};
            } else{
                return rvAdapter.onCreateViewHolder(parent,viewType);
            }
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            if(position == 0){
                return;
            } else if(position == rvAdapter.getItemCount() + 1){
                return;
            } else{
                rvAdapter.onBindViewHolder(((RvAdapter.ViewHolder)holder), position - 1);
            }
        }

        @Override
        public int getItemCount() {
            return rvAdapter.getItemCount()+2;
        }

        public ArrayList<String> getDatas(){
            return rvAdapter.datas;
        }

        public void addNum(){
            rvAdapter.addItemView();
            notifyDataSetChanged();
        }

        public void delete(){
            rvAdapter.deleteItem();
            notifyDataSetChanged();
        }

        public void addHeaderView(View view){
            this.mHeaderView = view;
        }
        public void addFooterView(View view){
            this.mFooterView = view;
        }
    }
}
